/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cadastro;

import java.util.ArrayList;
import javax.swing.JOptionPane;
import utilitario.Funcionario;
import utilitario.Arquivo;
import utilitario.formEditaFuncionario;

/**
 *
 * @author marcos
 */
public class funcionario extends javax.swing.JFrame {

    public ArrayList<Funcionario> lista_de_funcionarios = new ArrayList<>();

    /**
     * Creates new form funcionario
     */
    public funcionario() {
        initComponents();

    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        lbltelefone = new javax.swing.JLabel();
        txttelefone = new javax.swing.JTextField();
        lblcpf = new javax.swing.JLabel();
        lbllogin = new javax.swing.JLabel();
        txtcpf = new javax.swing.JTextField();
        txtlogin = new javax.swing.JTextField();
        cboxsexo = new javax.swing.JComboBox();
        txtendereco = new javax.swing.JTextField();
        lblsalario = new javax.swing.JLabel();
        lblendereco = new javax.swing.JLabel();
        txtsalario = new javax.swing.JTextField();
        txtnome = new javax.swing.JTextField();
        lblsexo = new javax.swing.JLabel();
        lblnome = new javax.swing.JLabel();
        cboxcargo = new javax.swing.JComboBox();
        jLabel8 = new javax.swing.JLabel();
        lblcargo = new javax.swing.JLabel();
        txtid = new javax.swing.JTextField();
        lblid = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tblfuncionarioscadastrados = new javax.swing.JTable();
        txtidentidade = new javax.swing.JTextField();
        jSeparator1 = new javax.swing.JSeparator();
        lblsenha = new javax.swing.JLabel();
        lblconfirmasenha = new javax.swing.JLabel();
        lblidentidade = new javax.swing.JLabel();
        txtconfirmasenha = new javax.swing.JPasswordField();
        txtsenha = new javax.swing.JPasswordField();
        btneditar = new javax.swing.JButton();
        btndeletar = new javax.swing.JButton();
        btncadastrar2 = new javax.swing.JButton();
        lblFundo = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setMinimumSize(new java.awt.Dimension(800, 600));
        setPreferredSize(new java.awt.Dimension(800, 600));
        setResizable(false);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowOpened(java.awt.event.WindowEvent evt) {
                formWindowOpened(evt);
            }
        });
        getContentPane().setLayout(null);

        lbltelefone.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        lbltelefone.setText("Telefone:");
        getContentPane().add(lbltelefone);
        lbltelefone.setBounds(310, 180, 60, 14);

        txttelefone.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        getContentPane().add(txttelefone);
        txttelefone.setBounds(240, 200, 140, 30);

        lblcpf.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        lblcpf.setText("CPF:");
        getContentPane().add(lblcpf);
        lblcpf.setBounds(680, 180, 30, 14);

        lbllogin.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        lbllogin.setText("Login:");
        getContentPane().add(lbllogin);
        lbllogin.setBounds(60, 300, 33, 14);

        txtcpf.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        getContentPane().add(txtcpf);
        txtcpf.setBounds(620, 200, 140, 30);

        txtlogin.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        getContentPane().add(txtlogin);
        txtlogin.setBounds(40, 320, 180, 30);

        cboxsexo.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        cboxsexo.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Feminino", "Masculino" }));
        getContentPane().add(cboxsexo);
        cboxsexo.setBounds(160, 260, 100, 30);

        txtendereco.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        getContentPane().add(txtendereco);
        txtendereco.setBounds(400, 200, 200, 30);

        lblsalario.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        lblsalario.setText("Salário:");
        getContentPane().add(lblsalario);
        lblsalario.setBounds(450, 240, 50, 14);

        lblendereco.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        lblendereco.setText("Endereço:");
        getContentPane().add(lblendereco);
        lblendereco.setBounds(450, 180, 60, 14);

        txtsalario.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        getContentPane().add(txtsalario);
        txtsalario.setBounds(400, 260, 130, 30);

        txtnome.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        getContentPane().add(txtnome);
        txtnome.setBounds(40, 200, 180, 30);

        lblsexo.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        lblsexo.setText("Sexo:");
        getContentPane().add(lblsexo);
        lblsexo.setBounds(180, 240, 40, 14);

        lblnome.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        lblnome.setText("Nome:");
        getContentPane().add(lblnome);
        lblnome.setBounds(120, 180, 40, 14);

        cboxcargo.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        cboxcargo.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Vendedor", "Contador", "Gerente" }));
        getContentPane().add(cboxcargo);
        cboxcargo.setBounds(280, 260, 100, 30);

        jLabel8.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabel8.setText("(apenas números)");
        getContentPane().add(jLabel8);
        jLabel8.setBounds(670, 270, 110, 14);

        lblcargo.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        lblcargo.setText("Cargo:");
        getContentPane().add(lblcargo);
        lblcargo.setBounds(310, 240, 40, 14);

        txtid.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        getContentPane().add(txtid);
        txtid.setBounds(550, 260, 120, 30);

        lblid.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        lblid.setText("ID");
        getContentPane().add(lblid);
        lblid.setBounds(600, 240, 20, 14);

        tblfuncionarioscadastrados.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        tblfuncionarioscadastrados.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null, null, null},
                {null, null, null, null, null, null},
                {null, null, null, null, null, null},
                {null, null, null, null, null, null},
                {null, null, null, null, null, null},
                {null, null, null, null, null, null},
                {null, null, null, null, null, null},
                {null, null, null, null, null, null},
                {null, null, null, null, null, null}
            },
            new String [] {
                "ID", "Nome", "Cargo", "Telefone", "Endereço", "CPF"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }
        });
        tblfuncionarioscadastrados.setMinimumSize(new java.awt.Dimension(720, 144));
        tblfuncionarioscadastrados.setPreferredSize(new java.awt.Dimension(720, 144));
        jScrollPane1.setViewportView(tblfuncionarioscadastrados);

        getContentPane().add(jScrollPane1);
        jScrollPane1.setBounds(40, 410, 720, 110);

        txtidentidade.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        getContentPane().add(txtidentidade);
        txtidentidade.setBounds(40, 260, 100, 30);

        jSeparator1.setForeground(new java.awt.Color(0, 0, 0));
        getContentPane().add(jSeparator1);
        jSeparator1.setBounds(10, 370, 780, 30);

        lblsenha.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        lblsenha.setText("Senha:");
        getContentPane().add(lblsenha);
        lblsenha.setBounds(250, 300, 40, 14);

        lblconfirmasenha.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        lblconfirmasenha.setText("Confirme a senha:");
        getContentPane().add(lblconfirmasenha);
        lblconfirmasenha.setBounds(500, 300, 120, 14);

        lblidentidade.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        lblidentidade.setText("Identidade:");
        getContentPane().add(lblidentidade);
        lblidentidade.setBounds(60, 240, 65, 14);

        txtconfirmasenha.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        getContentPane().add(txtconfirmasenha);
        txtconfirmasenha.setBounds(440, 320, 180, 30);

        txtsenha.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        getContentPane().add(txtsenha);
        txtsenha.setBounds(240, 320, 180, 30);

        btneditar.setText("Editar");
        btneditar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btneditarActionPerformed(evt);
            }
        });
        getContentPane().add(btneditar);
        btneditar.setBounds(40, 530, 100, 30);

        btndeletar.setText("Deletar");
        getContentPane().add(btndeletar);
        btndeletar.setBounds(150, 530, 100, 30);

        btncadastrar2.setText("Cadastrar");
        btncadastrar2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btncadastrar2ActionPerformed(evt);
            }
        });
        getContentPane().add(btncadastrar2);
        btncadastrar2.setBounds(650, 320, 100, 30);

        lblFundo.setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagem/fundo_fornecedor.png"))); // NOI18N
        lblFundo.setText("jLabel1");
        getContentPane().add(lblFundo);
        lblFundo.setBounds(0, 10, 800, 600);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btncadastrar2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btncadastrar2ActionPerformed

        Funcionario funcionario = new Funcionario();
        funcionario.setNome(txtnome.getText());
        funcionario.setTelefone(txttelefone.getText());
        funcionario.setEndereco(txtendereco.getText());
        funcionario.setCpf(txtcpf.getText());
        funcionario.setIdentidade(txtidentidade.getText());
        funcionario.setSexo(cboxsexo.getSelectedIndex() + "");
        funcionario.setCargo(cboxcargo.getSelectedIndex() + "");
        funcionario.setLogin(txtlogin.getText());

        if ((txtsalario.getText().equals("") == false) && (txtid.getText().equals("") == false) && (txtsenha.getText().equals(txtconfirmasenha.getText()))) {
            funcionario.setSalario(Double.parseDouble(txtsalario.getText()));
            funcionario.setId(Integer.parseInt(txtid.getText()));
            funcionario.setSenha(txtsenha.getText());
            System.out.println("Passou");
            if (Funcionario.isCadastravel(lista_de_funcionarios, funcionario)) {
                lista_de_funcionarios.add(funcionario);
                preencherTabela(lista_de_funcionarios);
                Arquivo.armazenarFuncionario(lista_de_funcionarios);
            }else{
                JOptionPane.showMessageDialog(this, "Existem campos vazios ou informações incorretas");
            }


        } else {
            JOptionPane.showMessageDialog(null, "Existem informações incorretas!");
        }
    }//GEN-LAST:event_btncadastrar2ActionPerformed

    public void preencherTabela(ArrayList<Funcionario> lista) {
        int tamanho = lista.size();
        for (int i = 0; i < tamanho; i++) {
            tblfuncionarioscadastrados.getModel().setValueAt(lista.get(i).getId() + "", i, 0);
            tblfuncionarioscadastrados.getModel().setValueAt(lista.get(i).getNome(), i, 1);
            tblfuncionarioscadastrados.getModel().setValueAt(lista.get(i).getCargo(), i, 2);
            tblfuncionarioscadastrados.getModel().setValueAt(lista.get(i).getTelefone(), i, 3);
            tblfuncionarioscadastrados.getModel().setValueAt(lista.get(i).getEndereco(), i, 4);
            tblfuncionarioscadastrados.getModel().setValueAt(lista.get(i).getCpf(), i, 5);
        }
    }

    private void btneditarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btneditarActionPerformed
        if (lista_de_funcionarios.isEmpty() == false) {
            int posicao = tblfuncionarioscadastrados.getSelectedRow();

            formEditaFuncionario janela = new formEditaFuncionario();
            janela.getEditFuncionario(lista_de_funcionarios, lista_de_funcionarios.get(posicao), posicao);
            janela.setVisible(true);
        }
    }//GEN-LAST:event_btneditarActionPerformed

    private void formWindowOpened(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowOpened
        lista_de_funcionarios = Arquivo.lerFuncionario();
        if (lista_de_funcionarios.isEmpty() == true) {
            System.out.println("NULLLLLLLLL");
        }
        if (lista_de_funcionarios.isEmpty() == false) {
            preencherTabela(lista_de_funcionarios);
        }
    }//GEN-LAST:event_formWindowOpened

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(funcionario.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(funcionario.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(funcionario.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(funcionario.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>


        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new funcionario().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btncadastrar2;
    private javax.swing.JButton btndeletar;
    private javax.swing.JButton btneditar;
    private javax.swing.JComboBox cboxcargo;
    private javax.swing.JComboBox cboxsexo;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JLabel lblFundo;
    private javax.swing.JLabel lblcargo;
    private javax.swing.JLabel lblconfirmasenha;
    private javax.swing.JLabel lblcpf;
    private javax.swing.JLabel lblendereco;
    private javax.swing.JLabel lblid;
    private javax.swing.JLabel lblidentidade;
    private javax.swing.JLabel lbllogin;
    private javax.swing.JLabel lblnome;
    private javax.swing.JLabel lblsalario;
    private javax.swing.JLabel lblsenha;
    private javax.swing.JLabel lblsexo;
    private javax.swing.JLabel lbltelefone;
    private javax.swing.JTable tblfuncionarioscadastrados;
    private javax.swing.JPasswordField txtconfirmasenha;
    private javax.swing.JTextField txtcpf;
    private javax.swing.JTextField txtendereco;
    private javax.swing.JTextField txtid;
    private javax.swing.JTextField txtidentidade;
    private javax.swing.JTextField txtlogin;
    private javax.swing.JTextField txtnome;
    private javax.swing.JTextField txtsalario;
    private javax.swing.JPasswordField txtsenha;
    private javax.swing.JTextField txttelefone;
    // End of variables declaration//GEN-END:variables
}
